﻿using System;
using System.Collections.Generic;

using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class AddHtml : Portal.Interface.WebPart
    {
        protected global::FredCK.FCKeditorV2.FCKeditor FCKeditor1;
        public override string WebPartId
        {
            get
            {
                return "AddHtml";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Thêm mới bài viết");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }


        public override void LoadSubControls(System.Web.UI.WebControls.PlaceHolder pl,System.Web.UI.Page Page, LoadParamCls LoadParamCls)
        {
            FCKeditor1 = new FredCK.FCKeditorV2.FCKeditor();
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Portal.CMS.AddHtml));
            string belongSite = (string)Environments.Request("belongSite");
          
            string SelectLanguageText =
                 "<select id=divSelectLanguage>";
            string[] Languages = Environments.GetLanguages();
            for (int iIndex = 0; iIndex < Languages.Length; iIndex++)
            {
                if (Environments.ActiveLanguage.Equals(Languages[iIndex]))
                {
                    SelectLanguageText += "<option selected value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
                else
                {
                    SelectLanguageText += "<option value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
            }
            SelectLanguageText += "</select>";
            string RetHtml =
                Portal.Environment.WebPartUtilities.AttachJavascript("Portal.CMS", "Html.js") +
                             Portal.Environment.Environments.GetHeaderFormDialog(ResourceBLL.GetText(WebPartId,"AddHtmlFormTitle","Thêm mới bài viết"))+
                             "<input id=txtBelongSite type=hidden value=\"" + belongSite + "\">" +
                             "      <table>" +
                             "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "AddHtmlSelectLanguage", "Ngôn ngữ") + "</td>" +
                             "              <td>" + SelectLanguageText + "</td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId, "AddHtmlAlias", "Alias") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtAlias></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td class=cssCol1>" + ResourceBLL.GetText(WebPartId,"AddHtmlSubject","Tiêu đề") + "</td>" +
                             "              <td><input class=cssTextBoxDialog id=txtSubject></td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td colspan=2 class=cssTdFckEditor>";
            pl.Controls.Add(new System.Web.UI.LiteralControl(RetHtml));
            FCKeditor1.Height=600;
            FCKeditor1.Width = 780;
            FCKeditor1.BasePath = "fckeditor/";
            pl.Controls.Add(FCKeditor1);
            System.Web.HttpContext.Current.Session["AddHtmlFckEditor"] = FCKeditor1;
            RetHtml=
                             "</td>" +
                             "          </tr>" +
                             "          <tr>" +
                             "              <td class=cssCol1></td>" +
                             "              <td><input type=button class=cssSaveButton onclick=javascript:ClientSaveAddHtml(); value=\"" + ResourceBLL.GetText(WebPartId,"AddHtmlSaveButton","Ghi lại") + "\"><input type=button class=cssCancelButton onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId,"AddHtmlCancelButton","Thoát") + "\"></td>" +
                             "          </tr>" +
                             "      </table>" +
                             Portal.Environment.Environments.GetFooterFormDialog();
            
            pl.Controls.Add(new System.Web.UI.LiteralControl(RetHtml));
        }



        [AjaxPro.AjaxMethod]
        public string ServerSideAddHtml(string belongSite,string ActiveLanguage,string Alias,string Subject,string Body)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                if (Subject.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId,"EnterSubject","Xin vui lòng nhập tiêu đề bài viết!"));
                //if (Body.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId,"InputBody","Xin vui lòng nhập nội dung bài viết!"));

                Body = System.Web.HttpUtility.HtmlDecode(Body);

                string HtmlId = new HtmlBLL().AddHtml(null, belongSite, ActiveLanguage, Alias,Subject, Body);
                
                if (Alias.Trim().ToLower().Equals("InfoBottom".ToLower()))
                {
                    System.Web.HttpContext.Current.Session["InfoBottom"] = Body;
                }
                if (Alias.Trim().ToLower().Equals("InfoTop".ToLower()))
                {
                    System.Web.HttpContext.Current.Session["InfoTop"] = Body;
                }
                return ResourceBLL.GetText(WebPartId, "MessageAddOk", "Tạo bài viết mới thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }
    }
}